//
//  ViewController.m
//  location
//
//  Created by Dongxiang Xu on 2024/4/12.
//

#import "ViewController.h"
#import "XDXLocationManager.h"
#import "XDXBaseTabBarViewController.h"

@interface ViewController ()


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(active) name:Noti_Active object:nil];
              
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:UserId] integerValue]>0 && [[[NSUserDefaults standardUserDefaults] objectForKey:AccessToken] length]>0) {
        [self getUserInfo];
    }
    
    [[XDXLocationManager shareInstance] loadData];
    [[XDXLocationManager shareInstance] initialize];
    [[XDXLocationManager shareInstance] getUserLocations];
    [[XDXLocationManager shareInstance].locationManager startUpdatingHeading];
    [[XDXLocationManager shareInstance].locationManager startUpdatingLocation];
    [[XDXLocationManager shareInstance].locationManager startMonitoringSignificantLocationChanges];
    [[XDXLocationManager shareInstance].locationManager startMonitoringVisits];
}

- (void)getUserInfo
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    CryptoHTTPKit *manager = [[CryptoHTTPKit alloc] init];
    [manager encryptedPOST:@"/system/user/getInfo"
    parameters:param
    success:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue]==200 && [responseObject[@"data"][@"nickName"] isKindOfClass:[NSString class]] && [responseObject[@"data"][@"nickName"] length]>0) {
            [[NSUserDefaults standardUserDefaults] setValue:responseObject[@"data"][@"nickName"] forKey:NickName];
            [[NSUserDefaults standardUserDefaults] synchronize];
            [[NSUserDefaults standardUserDefaults] setValue: responseObject[@"data"][@"userId"] forKey:UserId];
            [[NSUserDefaults standardUserDefaults] synchronize];
            [XDXShareInstance shareInstance].userId = responseObject[@"data"][@"userId"];
            [XDXSocket shareInstance].userId = [XDXShareInstance shareInstance].userId;
            [self getGroupList];
        }else if ([[[NSUserDefaults standardUserDefaults] objectForKey:UserId] integerValue]>0){
            [XDXShareInstance shareInstance].userId = [[NSUserDefaults standardUserDefaults] objectForKey:UserId];
            [XDXSocket shareInstance].userId = [XDXShareInstance shareInstance].userId;
        }
    }
    failure:^(NSError *aError) {
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:UserId] integerValue]>0){
            [XDXShareInstance shareInstance].userId = [[NSUserDefaults standardUserDefaults] objectForKey:UserId];
            [XDXSocket shareInstance].userId = [XDXShareInstance shareInstance].userId;
        }
    }];
}

- (void)active
{    
    [XDXLocationManager shareInstance].UIApplicationLaunchOptionsLocationKey = NO;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:UserId] integerValue]>0 && [[[NSUserDefaults standardUserDefaults] objectForKey:AccessToken] length]>0) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [[XDXSocket shareInstance] close];        
            XDXBaseTabBarViewController *tb = [[XDXBaseTabBarViewController alloc] init];
            [UIApplication sharedApplication].keyWindow.rootViewController = tb;
            [[UIApplication sharedApplication].keyWindow makeKeyAndVisible];
        });
    }
}

#pragma mark - 网络请求
- (void)getGroupList
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    CryptoHTTPKit *manager = [[CryptoHTTPKit alloc] init];
    [manager encryptedPOST:@"/system/group/grouplist"
    parameters:param
    success:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue]==200 && [responseObject[@"data"] count]>0) {
            [XDXShareInstance shareInstance].groupId = [responseObject[@"data"] firstObject][@"id"];
            [self getGroupInfo];
        }
    }
    failure:^(NSError *aError) {
        
    }];
}

- (void)getGroupInfo
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:[XDXShareInstance shareInstance].groupId forKey:@"groupId"];
    CryptoHTTPKit *manager = [[CryptoHTTPKit alloc] init];
    [manager encryptedPOST:@"/system/group/userinfo"
    parameters:param
    success:^(NSDictionary *responseObject) {
       
    }
    failure:^(NSError *aError) {
        
    }];
}

@end
